//
//  SEGActivityLotteryBubbleButton.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/25.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityLotteryBubbleButton.h"
#import "SEGBussinessConstHeader.h"
#import "SEGProfileManager.h"

#if __has_include(<CommonMediator/SEGMediatorHeader.h>)
#import <CommonMediator/SEGMediatorHeader.h>
#else
#import "SEGMediatorHeader.h"
#endif

//标签大小
static const CGFloat countHeight = 14.0;

@interface SEGActivityLotteryBubbleButton()
/**
 按钮
 */
@property (nonatomic, strong)UIButton *bubbleButton;

/**
 数字标签
 */
@property (nonatomic, strong)UILabel *countLabel;
@end

@implementation SEGActivityLotteryBubbleButton
{
    ActicityBubbleButtonBlock _block;
}

+ (SEGActivityLotteryBubbleButton *)sharedView
{
    static SEGActivityLotteryBubbleButton *_instance;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _instance = [[self alloc] initWithFrame:CGRectZero];
    });
    return _instance;
}

+ (void)showLotteryBubbleWithBottomPadding:(CGFloat)bottomPadding block:(ActicityBubbleButtonBlock)block
{
    [[SEGActivityLotteryBubbleButton sharedView] showWithBottomPadding:bottomPadding block:block];
}

+ (void)hiddenLotteryBubble
{
    [[SEGActivityLotteryBubbleButton sharedView] hiddenBubble];
}

#pragma mark -初始化
-(instancetype)init{
    if (self == [super init]) {
        [self seg_setupUI];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self seg_setupUI];
    }
    return self;
}

-(void)seg_setupUI{
//    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_prize")];
    CGFloat height = image.size.height;
    CGFloat width = image.size.width;
    CGFloat buttonWidth = width;
    
    [self addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(3);
        make.left.mas_equalTo(image.size.width-11);
        make.height.mas_equalTo(countHeight);
    }];
    
    self.frame = CGRectMake(LYScreenWidth() - buttonWidth - 15 ,LYScreenHeight() - 123 - height - LYSafeAreaBottom(), buttonWidth, height);
    self.bubbleButton.frame = self.bounds;
}

- (void)showWithBottomPadding:(CGFloat)bottomPadding block:(ActicityBubbleButtonBlock)block
{
    _block = block;
    //获取本地抽奖信息
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",kActivityLotteryInfoArrayNew, kAccountUserID]];
    if(!array || array.count == 0) {//新路径获取不到，获取老数据
        array = [[NSUserDefaults standardUserDefaults] objectForKey:kActivityLotteryInfoArray];
    }
    NSUInteger arrayCount = array.count;
    if (arrayCount == 0) {
        self.hidden = YES;
    }else {
        self.hidden = NO;
        [self.superview bringSubviewToFront:self];
        
//        [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:self];
//        //防止签到按钮显示在升级弹窗之上
//        [[[UIApplication sharedApplication] keyWindow].subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            if ([obj isKindOfClass:NSClassFromString(@"SEGUpdateAppView")]) {
//                [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:obj];
//                *stop = YES;
//            }
//        }];
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_prize")];
        CGFloat height = image.size.height;
        CGFloat width = image.size.width;
        CGFloat buttonWidth = width;
        
        self.frame = CGRectMake(LYScreenWidth() - buttonWidth - 15 ,LYScreenHeight() - bottomPadding - height - LYSafeAreaBottom(), buttonWidth, height);
        self.bubbleButton.frame = self.bounds;
    }
}



// 更新签到气泡展示状态
- (void)updateShowStatusWithBottomPadding:(CGFloat)bottomPadding {
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_prize")];
    CGFloat height = image.size.height;
    CGFloat width = image.size.width;
    CGFloat buttonWidth = width;
    
    // 修改布局
    if (self.superview) {
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-bottomPadding);
            make.right.mas_equalTo(-15);
            make.width.mas_equalTo(buttonWidth);
            make.height.mas_equalTo(height);
        }];
    }
//    self.frame = CGRectMake(LYScreenWidth() - buttonWidth - 15 ,LYScreenHeight() - bottomPadding - height - LYSafeAreaBottom(), buttonWidth, height);
    
    //获取本地抽奖信息
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",kActivityLotteryInfoArrayNew, kAccountUserID]];
    if(!array || array.count == 0) {//新路径获取不到，获取老数据
        array = [[NSUserDefaults standardUserDefaults] objectForKey:kActivityLotteryInfoArray];
    }
    NSUInteger arrayCount = array.count;
    if (arrayCount == 0) {
        self.hidden = YES;
    } else {
        self.hidden = NO;
        if (arrayCount > 1) {
            self.countLabel.text = @(arrayCount).stringValue;
            CGFloat countLabelWidth = countHeight;
            if (arrayCount > 10) {
                if (arrayCount > 99) {
                    self.countLabel.text = @"99+";
                }
                countLabelWidth = MAX(ceil([self.countLabel.text widthForFont:kFont_10] + 8), countHeight);
            }
            [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(countLabelWidth);
            }];
            self.countLabel.hidden = NO;
        } else {
            self.countLabel.hidden = YES;
        }
    }
    [self.superview bringSubviewToFront:self];
}

- (void)hiddenBubble
{
    self.hidden = YES;
    [[[UIApplication sharedApplication] keyWindow] sendSubviewToBack:self];
}

- (void)seg_pressed
{
    // 直接跳转 action 里面有做处理
    UIViewController *lotteryVc = [[SEGUhomeMediator sharedInstance] seg_initActivityTriggerLotteryViewController:nil];
    //vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:lotteryVc animated:YES];
}

- (UIButton *)bubbleButton
{
    if (!_bubbleButton) {
        //抽奖按钮
        UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_prize")];
        UIEdgeInsets insets = UIEdgeInsetsMake(0, image.size.width - 1, 0, 0);
        UIImage *lotteryImage = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
        
        _bubbleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bubbleButton setBackgroundImage:lotteryImage forState:UIControlStateNormal];
        [_bubbleButton addTarget:self action:@selector(seg_pressed) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_bubbleButton];
        [self sendSubviewToBack:_bubbleButton];
        [_bubbleButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _bubbleButton;
}

- (UILabel *)countLabel
{
    if (!_countLabel) {
        //显示数字
        UILabel *label = [[UILabel alloc] init];
        label.font = kFont_10;
        label.textColor = UIColor.whiteColor;
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = kColor_S1;
        label.layer.masksToBounds = YES;
        label.layer.cornerRadius = countHeight / 2.0;
        _countLabel = label;
    }
    return _countLabel;
}

@end
